var app = getApp()
var utils = require('../../utils/utils.js')
Page( {
  data: {
    addressee: null,
    mobile:null,
    productInfo:null,
    homeAddress: null
  },
   //初始化购物车信息
  onLoad: function() {
    wx.showLoading({
      title: '加载中...',
    })
    //console.log("==order-init==")
    var that = this
    //=== start ===
    wx.login({
      success: function (res) {
        var param = {"code":res.code,"appId":app.globalData.appId}
        wx.request({
          url: utils.sysConst.getFastOrderUserInfoPath,
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
          },
          data: param,
          dataType: "json",
          success: function(res) {
            if (res.data.code == 1){
              //设置全局用户信息-openid
              app.globalData.openid = res.data.data.openid
              console.log("app.globalData.openid-cart",app.globalData.openid)
              if (res.data.data.mobile != null){
                that.setData({
                  productInfo: null,
                  addressee :res.data.data.addressee,
                  mobile :res.data.data.mobile,
                  homeAddress :res.data.data.homeAddress
                })
              }
            }else{
              wx.showToast({
                title:"获取用户信息失败："+res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    });
    wx.hideLoading();
    //=== end ===
  },

  order: function(event){
    var homeAddress = event.detail.value.homeAddress;
    var addressee =event.detail.value.addressee;
    var mobile =event.detail.value.mobile
    var productInfo =event.detail.value.productInfo
   if (addressee == null || addressee ==""){
      wx.showToast({
        title:"请输入收货人",
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (mobile == null || mobile ==""){
      wx.showToast({
        title:"请输入收货人手机",
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (homeAddress == null || homeAddress ==""){
      wx.showToast({
        title:"请输入收货地址",
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (productInfo == null || productInfo ==""){
      wx.showToast({
        title:"请录入你需要购买的产品",
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    //设置提交按钮不可用
    this.setData({
      disabled: !this.data.disabled,
      loading: !this.data.loading
    })
    wx.showLoading({
      title: '处理中...',
    })
    var param = {"openId":app.globalData.openid
      ,"homeAddress":homeAddress
      ,"addressee":addressee
      ,"mobile":mobile
      ,"productInfo":productInfo
    }
    wx.request({
      url: utils.sysConst.fastOrderPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 1){
          wx.showToast({
            title: '下单成功',
            icon: 'success',
            duration: 2000
          })
          wx.navigateTo({
            url: '/pages/orderSuccess/orderSuccess?type=order',
          })
        }else{
          wx.showToast({
            title:res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    // 设置提交按钮可用
    this.setData({
      disabled: !this.data.disabled,
      loading: !this.data.loading
    })
  },
  //重新打开
  onShow(){
    var that = this
    that.setData({
      productInfo: null,
    })
  },

})
